package org.example.course.controller;

import org.example.course.common.DataGridView;
import org.example.course.common.ResultObj;
import org.example.course.common.utils.StringUtils;
import org.example.course.domain.Graduation;
import org.example.course.dto.GraduationModel;
import org.example.course.service.GraduationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Classname GraduationController
 * @Description 毕业要求
 * @Date 2020/5/8 15:28
 * @Created by
 */
@RestController
@RequestMapping("graduations")
public class GraduationController {

    @Autowired
    private GraduationService service;


    /**
     * 分页查询
     *
     * @param graduationModel
     * @return
     */
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public DataGridView page(GraduationModel graduationModel) {
        DataGridView dataGridView = service.findPage(graduationModel);
        return dataGridView;
    }

    /**
     * 创建
     *
     * @param graduation
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResultObj create(Graduation graduation) {
        if (graduation == null) {
            return ResultObj.CREATE_ERROR;
        }
        if (service.create(graduation)) {
            return ResultObj.CREATE_SUCCESS;
        }
        return ResultObj.CREATE_ERROR;
    }


    /**
     * 更新
     *
     * @param graduation
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResultObj update(Graduation graduation) {
        if (graduation == null) {
            return ResultObj.UPDATE_ERROR;
        }
        if (service.update(graduation)) {
            return ResultObj.UPDATE_SUCCESS;
        }
        return ResultObj.UPDATE_ERROR;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResultObj delete(@PathVariable String id) {
        if (!StringUtils.isNumeric(id)) {
            return ResultObj.COMMON_WARN_ID_SHOULD_INTEGER;
        }
        if (service.delete(id)) {
            return ResultObj.DELETE_SUCCESS;
        }
        return ResultObj.DELETE_ERROR;
    }


}
